package com.kexio.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
 * 统一的 Jackson 配置管理
 * 
 * 优雅地管理所有 ObjectMapper Bean，避免配置分散和冲突
 * 
 * @author Kexio Team
 */
@Configuration
public class JacksonConfiguration {

    /**
     * HTTP 专用 ObjectMapper - 系统主要的JSON处理器
     * 
     * @Primary 确保 Spring MVC 和其他HTTP组件使用此配置
     */
    @Bean
    @Primary
    public ObjectMapper httpObjectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder
            .modulesToInstall(new JavaTimeModule())
            .build();
    }

    /**
     * Redis 专用 ObjectMapper - 缓存系统专用
     * 
     * 配置更宽松的反序列化规则，适合缓存场景
     */
    @Bean("redisObjectMapper")  
    public ObjectMapper redisObjectMapper() {
        return new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    /**
     * WebSocket 专用 ObjectMapper - 实时通信专用
     * 
     * 配置简洁快速的序列化，适合实时通信
     */
    @Bean("webSocketObjectMapper")
    public ObjectMapper webSocketObjectMapper() {
        return new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }
}